package com.lyx.bookmanager.util;



import com.lyx.bookmanager.pojo.WebRestResult;
import com.lyx.bookmanager.util.Tools;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.tools.Tool;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author lhq
 */

public class FileUtil {

    public static String FilePathUrl = "http://localhost:8080/";


    public static String upload(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return "";
        }
        String s = writeFile(file, file.getOriginalFilename(), "C://library//");
        if (!Tools.isEmpty(s)) {
            return FilePathUrl + file.getOriginalFilename();
        }
        return "";
    }
    /**
     * 用于校验文件格式,校验文件大小
     * @param file
     * @return
     */
    public static WebRestResult validateFile(MultipartFile file) {
        WebRestResult result = new WebRestResult();

        String fileType = ".pdf,.jpg1024,.jpeg,.png,.bmp";
        //5 * 1024 * 1024
        String fileSizeLimit = "5242880";
        if (!file.isEmpty()) {
            String oldFileName = file.getOriginalFilename();
            if (Tools.isEmpty(oldFileName)) {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("文件格式错误：无文件名！");
                return result;
            }
            if (!FileUtil.checksSuffix(oldFileName, fileType)) {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("文件格式错误：文件格式错误：非法后缀名！");
                return result;
            }
//                byte[] bytes = new byte[0];
//                bytes = file.getBytes();
//                if (!FileUtil.checkFileSize(bytes, fileSizeLimit)) {
//                    result.setResult(WebRestResult.FAILURE);
//                    result.setMessage("文件太大");
//                    return result;
//                }
        } else {
            result.setMessage("文件不能为空！");
            result.setResult(WebRestResult.FAILURE);
        }
        result.setResult(WebRestResult.SUCCESS);
        result.setMessage("文件校验成功！");
        return result;
    }

    //保存文件
    public static String writeFile(MultipartFile file, String fileName , String fileUploadLocation)throws IOException{
        //生成上传路径
        String realPath = fileUploadLocation + "/" + fileName;

        File newFile = new File(realPath);

        //判断文件目录是否存在
        if (!newFile.getParentFile().exists()) {
            newFile.getParentFile().mkdirs();
        }

        if (!newFile.exists()) {
            newFile.createNewFile();
        }
        //保存文件
        file.transferTo(newFile);
        return realPath;
    }

    //删除文件
    public static void delFile(String fileUrl) {
        if(!Tools.isEmpty(fileUrl)){
            File file = new File(fileUrl);
            if (file.exists() && file.isFile()) {
                file.delete();
            }
        }
    }

    //检查文件后缀
    public static Boolean checksSuffix(String fileName,String fileType){
        if (fileName.indexOf(".") == -1) {
            return false;
        }
        String suffix = fileName.substring(fileName.indexOf(".")).toLowerCase();

        if (fileType.indexOf(suffix) == -1) {
            return false;
        }
        return true;
    }

    //检查文件大小
    public static Boolean checkFileSize(byte[] bytes ,String fileSizeLimit){
        int fileSize = Integer.parseInt(fileSizeLimit);
        if (bytes.length > fileSize) {
            return false;
        }
        else {
            return true;
        }
    }

    //检查文件是否存在
    public static boolean existFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            return true;
        }
        return false;
    }
}
